# -*- coding: utf-8 -*-
# coding:unicode_escape


import time
import math
from parking_lot.car import Car

#time.asctime()


class Parking(object):
    """
    定义停车场类，可以停车、查询车辆、取车、收费
    """

    def __init__(self, max_cars):
        super(Parking, self).__init__()
        self.unit_price_per_hour = 6
        self.max_cars = max_cars
        self.car_list = []

    def charges(self, hours):
        """
        收费系统，不满1小时按1小时收费
        """
        
        if hours <= 0:
            return False
        
        costs = hours * self.unit_price_per_hour
        return costs

    def park(self, plate_number):
        if len(self.car_list) == self.max_cars:
            print("对不起，没有空车位，请去其它停车场。")
            return False
        
        if len(self.car_list) < self.max_cars:
            print("欢迎光临！本停车场还有 %d 停车位。" % (self.max_cars - len(self.car_list),))
            plate_number = plate_number
            starttime = time.time()
            car = Car(plate_number, starttime)
            self.car_list.append(car)
            print("车牌号为 %s 的车辆已停入停车场，停入时间 %s 。" % (plate_number, time.ctime(starttime)))

    def pick_up(self, plate_number):
        starttime = self.query_car(plate_number)
        if not starttime:
            print("没有查询到车牌号为 %s 的车辆，请联系管理员。" % (plate_number,))
            return False
        
        stoptime = time.time()
        park_hours = (stoptime - starttime)/3600
        park_hours = math.ceil(park_hours)
        costs = self.charges(park_hours)
        print("收费: %d 元。谢谢！再见！" % (costs,))
        self.remove_car(plate_number)

    def query_car(self, plate_number):
        for car in self.car_list:
            if plate_number == car.get_plate_number():
                print("%s 停入时间为 %s" % (plate_number, time.ctime(car.get_starttime())))
                return car.get_starttime()
                break

    def remove_car(self, plate_number):
        for car in self.car_list:
            if plate_number == car.get_plate_number():
                self.car_list.remove(car)
                return True
                break
